package com.csthink.web.interfaces;


import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * logback 配置测试控制器
 *
 * @author <a href="mailto:security.2009@live.cn">Mars</a>
 * @since 2023-09-10
 */
@Api(tags = "Test 模块")
@Slf4j
@RestController
@RequestMapping("/log")
public class LogTestController {

    private final Logger logger = LoggerFactory.getLogger(LogTestController.class);

    @GetMapping("/")
    public String test() {

        logger.trace("trace msg");
        logger.debug("debug msg");
        logger.info("info msg");
        logger.warn("warn msg");
        logger.error("error msg");

        return "success";
    }

    @GetMapping("/t2")
    public String test2() {

        log.trace("trace msg");
        log.debug("debug msg");
        log.info("info msg");
        log.warn("warn msg");
        log.error("error msg");

        return "success";
    }

    @GetMapping("/t3")
    public String test3() {
        log.debug("Main thread debug .....");
        Map<String, String> copyOfContextMap = MDC.getCopyOfContextMap();

        new Thread(() -> {
            try {
                MDC.setContextMap(copyOfContextMap);
                log.debug("Sub Thread debug ===================");
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }).start();

        return "success";
    }
}
